<?php
!defined('QAPP') and exit('Forbidden');
$must_exts = array(
    "json" => "编码和解码JSON数据格式",
    "mbstring" => "处理中文及多字节文字",
    "gd" => "生成图形验证码",
    "date" => "处理时间日期",
    "session" => "处理用户会话",
    "iconv" => "字符编码转换",
);
$need_exts = array(
    "pdo_mysql|pdo_sqlite" => "必须开启pdo_mysql或pdo_sqlite组件，支持操作mysql或sqlite数据库",
    "intl" => "QAPP可选支持国际化功能时，当开启时需要此组件",
    "memcache|memcached" => "QAPP支持将数据存入memcached缓存中，依赖memcache或memcached组件",
    "redis" => "QAPP支持将数据存入redis缓存中，依赖redis组件",
    "fileinfo" => "获取文件MIME、编码、格式等信息，主要用于对图片的处理时需要",
);

$ext_error = array();
$ext_warning = array();


foreach ($must_exts as $key => $val) {
    if (!ext_exist($key)) {
        $ext_error["php扩展|" . $key] = $val;
        unset($must_exts[$key]);
    }
}

$have_pdo_mysql = true;
$have_pdo_sqlite = true;

foreach ($need_exts as $keys => $val) {
    $keys_arr = explode('|', $keys);
    $all_keys_notexist = true;
    foreach ($keys_arr as $key) {
        if (!ext_exist($key)) {
            if ($key == "pdo_mysql") $have_pdo_mysql = false;
            if ($key == "pdo_sqlite") $have_pdo_sqlite = false;
            unset($need_exts[$keys]);
        }else{
            $all_keys_notexist = false;
            break;
        }
    }
    if ($all_keys_notexist) $ext_warning[$keys] = $val;
}

if (!$have_pdo_mysql && !$have_pdo_sqlite) {
    $ext_error['php扩展|pdo_mysql或pdo_sqlite'] = "QAPP使用mysql或sqlite，所以必须开启pdo_mysql或pdo_sqlite组件中的其中一个";
}

$upload_max_filesize = real_size_byte(ini_get('upload_max_filesize'));
$post_max_size = real_size_byte(ini_get('post_max_size'));
$max_upload_size = $upload_max_filesize > $post_max_size ? $post_max_size : $upload_max_filesize;
$max_upload_size_str = byte_format($max_upload_size);//文件最大尺寸

if ($max_upload_size <= 0) {
    $ext_error['文件上传限制|upload_max_filesize和post_max_size'] = "QAPP需要开启文件上传功能，请在php.ini中配置upload_max_filesize和post_max_size两个参数，一般允许上传的文件约20M左右。";
}


$writecheck = array(
    "application/cache" => "模板和数据库数据缓存目录",
    "application/data" => "sqlite数据库文件和sql数据库备份文件存放处",
    "application/controller" => "控制器存放目录",
    "application/languages" => "国际化语言存放目录",
    "logs" => "日志存放目录",
    "public/uploadfile" => "日志存放目录",
);

foreach ($writecheck as $key => $val) {
    if (!dir_test("../../" . $key)) {
        $ext_error["目录无法写入|" . $key] = $val;
    }
}


//dumpForInstall($extension);
?>
<div class="panel " id="phpversion">
    <div class="panel-heading">PHP版本</div>
    <div class="panel-body">
        <?php
        echo "<p>PHP版本：" . "<b class='label label-default'>" . PHP_VERSION . "</b></p>";
        if (version_compare(PHP_VERSION, "5.6.0", "<")) {
            $ext_error['PHP'] = "您当前的PHP版本低于5.6，QAPP只支持PHP5.6及以上版本";
            echo "<script>document.getElementById('phpversion').classList.add('panel-danger')</script>";
            echo "<p><b class='label label-danger'>{$ext_warning['PHP']}</b></p>";
        } else if (version_compare(PHP_VERSION, "7.0.0", "<")) {
            $ext_warning['PHP'] = "您当前的PHP版本低于7.0，QAPP在PHP7.0-8.1版本中将获得更大的性能，<b style='color: red'>Twig模板引擎在7.0以下版本无法运行</b>";
            echo "<script>document.getElementById('phpversion').classList.add('panel-warning')</script>";
            echo "<p><b class='label label-warning'>{$ext_warning['PHP']}</b></p>";
        } else if (version_compare(PHP_VERSION, "8.1.99", ">")) {
            $ext_warning['PHP'] = "您当前的PHP版本高于8.1，QAPP未能在此PHP版本中测试，QAPP在PHP7.0-8.1版本中将获得更大的性能";
            echo "<script>document.getElementById('phpversion').classList.add('panel-warning')</script>";
            echo "<p><b class='label label-warning'>{$ext_warning['PHP']}</b></p>";
        } else {
            echo "<script>document.getElementById('phpversion').classList.add('panel-success')</script>";
            echo "<p><b class='label label-success'>您的PHP版本符合要求，QAPP在PHP7.0-8.1版本中将获得更大的性能</b></p>";
        }

        ?>
    </div>
</div>

<div class="panel panel-info" id="otherinfo">
    <div class="panel-heading">系统信息</div>
    <div class="panel-body">
        <?php
        echo "<p>文件上传限制：" . "<b class='label label-default'>" . $max_upload_size_str . "</b>";
        echo " 运行方式：" . "<b class='label label-default'>" . php_sapi_name() . "</b>";
        echo " 操作系统：" . "<b class='label label-default'>" . php_uname('s') . "</b>";
        echo " 执行时间限制：" . "<b class='label label-default'>" . ini_get('max_execution_time') . "秒</b></p>";
        ?>
    </div>
</div>
<?php if (count($ext_error) > 0): ?>
    <div class="panel panel-danger">
        <div class="panel-heading">致命错误</div>
        <div class="panel-body">
            <p>如果存在该错误，您将不能继续安装，请先解决以下问题：</p>
            <table class="table table-striped table-bordered table-responsive">
                <tr>
                    <th>状态</th>
                    <th>检查项目</th>
                    <th>说明</th>
                </tr>
                <?php foreach ($ext_error as $key => $val):
                    $keyarr = explode('|', $key);
                    ?>
                    <tr>
                        <th><b class="label label-danger"><?php echo $keyarr[0] ?></b></th>
                        <th><?php echo $keyarr[1] ?></th>
                        <th><?php echo $val ?></th>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </div>
<?php endif; ?>


<?php if (count($ext_warning) > 0): ?>
    <div class="panel panel-warning">
        <div class="panel-heading">警告错误</div>
        <div class="panel-body">
            <p>警告错误，可以继续安装，但是在使用具体功能时，可能会出现错：</p>
            <table class="table table-striped table-bordered table-responsive">
                <tr>
                    <th>状态</th>
                    <th>检查项目</th>
                    <th>说明</th>
                </tr>
                <?php foreach ($ext_warning as $key => $val): ?>
                    <tr>
                        <th><b class="label label-warning">警告</b></th>
                        <th><?php echo $key ?></th>
                        <th><?php echo $val ?></th>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </div>
<?php endif;
if (count($ext_error) == 0) {
    session_set("step", 30);
    ?>
    <p style="margin: 30px;text-align: center">
        <a class="btn btn-primary" href="?step=30">下一步，Web服务器配置</a>
    </p>
    <?php
}
?>





